package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.dish_pojo.OrderItem;
import com.zhentao.service.OrderItemService;
import com.zhentao.mapper.OrderItemMapper;
import com.zhentao.mapper.DishInfoMapper;
import com.zhentao.mapper.DishSpecMapper;
import com.zhentao.mapper.DishTasteMapper;
import com.zhentao.vo.OrderItemVO;
import org.example.dish_pojo.DishInfo;
import org.example.dish_pojo.DishSpec;
import org.example.dish_pojo.DishTaste;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author Lenovo
* @description 针对表【order_item(订单菜品明细表)】的数据库操作Service实现
* @createDate 2025-08-14 11:01:42
*/
@Service
public class OrderItemServiceImpl extends ServiceImpl<OrderItemMapper, OrderItem>
    implements OrderItemService{

    @Autowired
    private DishInfoMapper dishInfoMapper;
    
    @Autowired
    private DishSpecMapper dishSpecMapper;
    
    @Autowired
    private DishTasteMapper dishTasteMapper;

    @Override
    public List<OrderItem> getByOrderId(Integer orderId) {
        QueryWrapper<OrderItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", orderId);
        return this.list(queryWrapper);
    }

    @Override
    public List<OrderItemVO> getOrderItemVOsByOrderId(Integer orderId) {
        // 获取订单明细
        List<OrderItem> orderItems = getByOrderId(orderId);
        
        // 转换为VO对象并填充关联信息
        return orderItems.stream().map(this::convertToVO).collect(Collectors.toList());
    }

    @Override
    public boolean removeByOrderId(Integer orderId) {
        QueryWrapper<OrderItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", orderId);
        return this.remove(queryWrapper);
    }
    
    /**
     * 将OrderItem转换为OrderItemVO并填充关联信息
     */
    private OrderItemVO convertToVO(OrderItem orderItem) {
        OrderItemVO vo = new OrderItemVO();
        
        // 复制基本属性
        vo.setItemId(orderItem.getItemId());
        vo.setOrderId(orderItem.getOrderId());
        vo.setDishId(orderItem.getDishId());
        vo.setSpecId(orderItem.getSpecId());
        vo.setTasteId(orderItem.getTasteId());
        vo.setQuantity(orderItem.getQuantity());
        vo.setUnitPrice(orderItem.getUnitPrice());
        vo.setSubtotal(orderItem.getSubtotal());
        
        // 查询菜品信息
        if (orderItem.getDishId() != null) {
            try {
                DishInfo dishInfo = dishInfoMapper.selectById(orderItem.getDishId());
                if (dishInfo != null) {
                    vo.setDishName(dishInfo.getName());
                } else {
                    vo.setDishName("菜品不存在");
                }
            } catch (Exception e) {
                System.err.println("查询菜品信息失败，菜品ID: " + orderItem.getDishId() + ", 错误: " + e.getMessage());
                vo.setDishName("查询失败");
            }
        } else {
            vo.setDishName("无菜品信息");
        }
        
        // 查询规格信息
        if (orderItem.getSpecId() != null) {
            try {
                DishSpec dishSpec = dishSpecMapper.selectById(orderItem.getSpecId());
                if (dishSpec != null) {
                    vo.setSpecName(dishSpec.getSpecName());
                } else {
                    vo.setSpecName("规格不存在");
                }
            } catch (Exception e) {
                System.err.println("查询规格信息失败，规格ID: " + orderItem.getSpecId() + ", 错误: " + e.getMessage());
                vo.setSpecName("查询失败");
            }
        } else {
            vo.setSpecName(null); // 规格为可选，返回null
        }
        
        // 查询口味信息
        if (orderItem.getTasteId() != null) {
            try {
                DishTaste dishTaste = dishTasteMapper.selectById(orderItem.getTasteId());
                if (dishTaste != null) {
                    vo.setTasteName(dishTaste.getTasteName());
                } else {
                    vo.setTasteName("口味不存在");
                }
            } catch (Exception e) {
                System.err.println("查询口味信息失败，口味ID: " + orderItem.getTasteId() + ", 错误: " + e.getMessage());
                vo.setTasteName("查询失败");
            }
        } else {
            vo.setTasteName(null); // 口味为可选，返回null
        }
        
        return vo;
    }
}




